package com.cat.dynamicPlanning;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/house-robber-ii/
 * @create 2025/8/27 16:10
 * @since JDK17
 */

public class Solution12 {
    public int f(int[] nums, int s, int e) {
        int[] dp = new int[nums.length];
        dp[s] = nums[s];
        dp[s + 1] = Math.max(nums[s], nums[s + 1]);
        for (int i = s + 2; i < e; i++)  {
            dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
        }
        return Math.max(dp[e - 1], dp[e - 2]);
    }


    public int rob(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        }
        if (nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        }
        int n = nums.length;
        return Math.max(f(nums, 0, n - 1), f(nums, 1, n));
    }
}
